<template>
  <div style="background-color: white">
    <div style="width: 400px;margin: 5% 10% 10% 37%">
      <h1 style="text-align: center; margin: 20px 0;">欢迎注册憨宠</h1>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="ruleForm.nickname"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="password">
          <el-input v-model="ruleForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email"></el-input>
        </el-form-item>
        <el-form-item style="margin-left: 20px ">
          <el-button type="primary" @click="reg('formName')">注册</el-button>&nbsp;
          <a href="/login">已有账号？去登录</a>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      url: '176.212.26.22:9090',
      ruleForm: {
        username: '',
        password: '',
        password2: '',
        nickname: '',
        email: '',
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        nickname: [
          {required: true, message: "请输入昵称", trigger: 'blur'},
          {min: 1, max: 6, message: "长度在1到6个字符", trigger: "blur"}
        ]
      }
    }
  },
  methods: {
    reg(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://' + this.url + '/user/register'
          console.log('url=' + url)
          /* 将请求参数更改为formData */
          let formData = this.qs.stringify(this.ruleForm);
          this.axios.post(url, formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message.success("注册完成,正在前往登录！");

              this.$router.push("/login")

              /*// 测试能否获取jwt
              let localJwt = localStorage.getItem("jwt");
              console.log("从localStorage中读取JWT："+localJwt);
*/
            } else if (responseBody.state == 40100) {
              this.$message.error(responseBody.message)
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

